import json
import sys
import scrapy
from scrapy import Request, Selector
import pandas as pd

sys.path.append('../spiders')
from YMX.items import TaobaoItem


class STY(scrapy.Spider):
    name = 'sty'
    allowed_domains = ["stylishguy.ie"]

    def start_requests(self):
        df = pd.read_excel(r'C:\Users\Administrator\Desktop\dazz.xlsx')
        for i, j in zip(df["category"], df["pageurl"]):
            # url = "https://harrimanclothing.com/products/" + i.split("/")[-1] + ".js"
            yield Request(url=j, callback=self.parse_1, cb_kwargs={'category': i})

    #
    # def parse(self, response, **kwargs):
    #     sels = Selector(response)
    #     sel_cates = sels.xpath('//li[@class="grid__item lvl-1 large-up--one-half"]')
    #     for i in sel_cates:
    #         url = "https://dazzledmv.com" + i.xpath('./a/@href').extract_first()
    #         print(url)
    #         yield Request(url=url, callback=self.parse_1)

    # 解析每个分类
    def parse_1(self, response, **kwargs):
        sels1 = Selector(response)
        sel_ceates = sels1.xpath('//li[@class="grid__item"]/div/div/div/div[2]/div//h3')
        for i in sel_ceates:
            item = TaobaoItem()  # 获取分类
            # item["category"] = kwargs['category']
            uri = i.xpath('./a/@href').extract_first().strip()
            urls = "https://stylishguy.ie/products/" + uri.split("/")[-1] + ".js"
            # item["PageUrl"] = "https://stylishguy.ie" + uri
            # yield Request(url=urls, callback=self.parse_json, cb_kwargs={'item': item})
            print(uri)

        try:
            next_url = sels1.xpath(
                "//a[@aria-label='Next page']/@href").extract_first()
            url_next = "https://stylishguy.ie" + next_url
            print("进行翻页")
            print(url_next)
            yield Request(url=url_next, callback=self.parse_1)
        except:
            print("没有下一页了")

    def parse_json(self, response, **kwargs):
        jsonresp = json.loads(response.text)
        item = kwargs['item']
        if jsonresp.get("compare_at_price_max") == 0:
            item["price"] = str(jsonresp.get('price'))
            item['sprice'] = ""
            str2 = "." + item["price"][-2:]
            item["price"] = item["price"][0:len(item["price"]) - 2]
            item["price"] = item["price"] + str2
        else:
            item['sprice'] = str(jsonresp.get("price"))
            item["price"] = str(jsonresp.get('compare_at_price_max'))
            str3 = "." + item["sprice"][-2:]
            str4 = "." + item["price"][-2:]
            item["price"] = item["price"][0:len(item["price"]) - 2]
            item["sprice"] = item["sprice"][0:len(item["sprice"]) - 2]
            item["sprice"] = item["sprice"] + str3
            item["price"] = item["price"] + str4
            if float(item['sprice']) >= float(item["price"]):
                item['sprice'] = ""
        item["title"] = jsonresp.get('title')
        item["image"] = "https:" + jsonresp.get('featured_image')
        item["gallery"] = ""
        try:
            images = jsonresp.get('images')
            for img in images[1:]:
                item["gallery"] += str("https:" + img)
                item["gallery"] += ";"
            item["gallery"] = item["gallery"][0:len(item["gallery"]) - 1]
        except:
            print("没有二级图片")
        item['sku'] = ""
        item["color"] = ""
        item['size'] = ""
        item['waist'] = ""
        item['length'] = ""
        try:
            options = jsonresp.get('options')
            for option in options:
                if (option['name'] == "Color") or (option['name'] == "COLOR") or (option['name'] == "color"):
                    colors = option['values']
                    for col in colors:
                        item["color"] += col
                        item["color"] += ","
                    item["color"] = item["color"][0:len(item["color"]) - 1]
        except:
            print("没有颜色")

        try:
            options1 = jsonresp.get('options')
            for option1 in options1:
                if (option1['name'] == "Size") or (option1['name'] == "size") or (option1['name'] == "SIZE"):
                    sizes = option1['values']
                    for size in sizes:
                        item["size"] += size
                        item["size"] += ","
                    item["size"] = item["size"][0:len(item["size"]) - 1]
        except:
            print("没有大小")

        try:
            options2 = jsonresp.get('options')
            for option2 in options2:
                if (option2['name'] == "Waist") or (option2['name'] == "waist") or (option2['name'] == "WAIST"):
                    waists = option2['values']
                    for waist in waists:
                        item["waist"] += waist
                        item["waist"] += ","
                    item["waist"] = item["waist"][0:len(item["waist"]) - 1]
        except:
            print("没有waist")

        try:
            options3 = jsonresp.get('options')
            for option3 in options3:
                if (option3['name'] == "Length") or (option3['name'] == "length") or (option3['name'] == "LENGTH"):
                    lengths = option3['values']
                    for length in lengths:
                        item["length"] += length
                        item["length"] += ","
                    item["length"] = item["length"][0:len(item["length"]) - 1]
        except:
            print("没有length")

        item["brand"] = jsonresp.get("vendor") or ""
        item['description'] = jsonresp.get("description") or ""
        yield item
